package com.burry.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import org.logicalcobwebs.proxool.ProxoolException;
import org.logicalcobwebs.proxool.configuration.JAXPConfigurator;

import applelew.util.Debug;
import applelew.util.UtilURL;

public class SQLProcessor {
	
	private Connection conn = null;;
	private PreparedStatement ps = null;
	private Statement st = null;
	
	static {
		try {
			//Debug.logInfo("begin");
			//Debug.logInfo("app: " + UtilURL.fromResource("dbconn.xml"));
			//File f = new File();
			
			JAXPConfigurator.configure(UtilURL.fromResource("dbconn.xml").getFile(), false);
			
		} catch (ProxoolException e) {
			Debug.logError(e);
			Debug.logError("DB Conf not ready.");
		}
	}
	
	public SQLProcessor(String name) throws Exception{
		conn = DriverManager.getConnection("proxool.burry");
		
	}
	
	public void prepareStatement(String sql) throws Exception{
		this.ps = conn.prepareStatement(sql);
	}
	
	public int executeUpdate() throws Exception{
		return ps.executeUpdate();
	}
	
	public void close() throws Exception{
		if(ps != null)
			ps.close();
		if(st != null)
			st.close();
		if(conn != null)
			conn.close();
	}
	
	public ResultSet executeQuery(String sql) throws Exception{
		this.st = conn.createStatement();
		ResultSet rs = st.executeQuery(sql);
		return rs;
	}
	
	public static void main(String[] args) throws Exception{
		//SQLProcessor sp = new SQLProcessor("mine");
		
	}
}
